package cn.zjyj.module.zjyj.service.deviceinstance;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.module.system.dal.mysql.dept.DeptMapper;
import cn.zjyj.module.system.dal.mysql.user.AdminUserMapper;
import cn.zjyj.module.zjyj.dal.bo.deviceinstance.DeviceInstanceBo;
import cn.zjyj.module.zjyj.dal.mysql.tenantunit.TenantUnitMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.zjyj.module.zjyj.controller.admin.deviceinstance.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstance.DeviceInstanceDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.deviceinstance.DeviceInstanceMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 设备实例 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DeviceInstanceServiceImpl implements DeviceInstanceService {

    @Resource
    private DeviceInstanceMapper deviceInstanceMapper;

    @Resource
    AdminUserMapper userMapper;
    @Resource
    TenantUnitMapper tenantUnitMapper;
    @Resource
    DeptMapper deptMapper;


    @Override
    public Long createDeviceInstance(DeviceInstanceSaveReqVO createReqVO) {
        // 插入
        DeviceInstanceDO deviceInstance = BeanUtils.toBean(createReqVO, DeviceInstanceDO.class);
        deviceInstanceMapper.insert(deviceInstance);
        // 返回
        return deviceInstance.getId();
    }

    @Override
    public void updateDeviceInstance(DeviceInstanceSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceInstanceExists(updateReqVO.getId());
        // 更新
        DeviceInstanceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceInstanceDO.class);
        deviceInstanceMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceInstance(Long id) {
        // 校验存在
        validateDeviceInstanceExists(id);
        // 删除
        deviceInstanceMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceInstanceListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceInstanceExists(ids);
        // 删除
        deviceInstanceMapper.deleteByIds(ids);
        }

    private void validateDeviceInstanceExists(List<Long> ids) {
        List<DeviceInstanceDO> list = deviceInstanceMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_INSTANCE_NOT_EXISTS);
        }
    }

    private void validateDeviceInstanceExists(Long id) {
        if (deviceInstanceMapper.selectById(id) == null) {
            throw exception(DEVICE_INSTANCE_NOT_EXISTS);
        }
    }

    @Override
    public DeviceInstanceDO getDeviceInstance(Long id) {
        return deviceInstanceMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceInstanceBo> getDeviceInstancePage(DeviceInstancePageReqVO pageReqVO) {
        PageResult<DeviceInstanceDO> deviceInstanceDOPageResult = deviceInstanceMapper.selectPage(pageReqVO);
        PageResult<DeviceInstanceBo> bean = BeanUtils.toBean(deviceInstanceDOPageResult, DeviceInstanceBo.class);
        if (CollUtil.isEmpty(bean.getList())) {
            return bean;
        }
        // 遍历 bean的list的ōwnerId，返回数组，需要去重
        List<Long> ownerIds = bean.getList().stream().map(DeviceInstanceBo::getOwnerId).distinct().collect(Collectors.toList());
        // 遍历 bean的list的unitId，返回数组，需要去重
        List<Long> unitIds = bean.getList().stream().map(DeviceInstanceBo::getUnitId).distinct().collect(Collectors.toList());
        // 遍历 bean的list的departmentId，返回数组，需要去重
        List<Long> departmentIds = bean.getList().stream().map(DeviceInstanceBo::getDepartmentId).distinct().collect(Collectors.toList());
        userMapper.selectBatchIds(ownerIds).forEach(user -> {
            bean.getList().stream().filter(deviceInstanceBo -> deviceInstanceBo.getOwnerId().equals(user.getId())).forEach(deviceInstanceBo -> deviceInstanceBo.setOwnerName(user.getUsername()));
        });
        tenantUnitMapper.selectBatchIds(unitIds).forEach(unit -> {
            bean.getList().stream().filter(deviceInstanceBo -> deviceInstanceBo.getUnitId().equals(unit.getId())).forEach(deviceInstanceBo -> deviceInstanceBo.setUnitName(unit.getName()));
        });
        deptMapper.selectBatchIds(departmentIds).forEach(dept -> {
            bean.getList().stream().filter(deviceInstanceBo -> deviceInstanceBo.getDepartmentId().equals(dept.getId())).forEach(deviceInstanceBo -> deviceInstanceBo.setDepartmentName(dept.getName()));
        });
        return bean;
    }

}